package com.whxy02.tolearn.app.controller;

import com.whxy02.tolearn.app.beans.*;
import com.whxy02.tolearn.app.resultObject.ResultState;
import com.whxy02.tolearn.app.service.*;
import com.whxy02.tolearn.app.utils.PackResult;
import com.whxy02.tolearn.app.utils.Upload;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class Admin {
    @Resource
    SubjectService subjectService;
    @Resource
    TagsService tagsService;
    @Resource
    QuestionTypeService questionTypeService;
    @Resource
    QuestionService questionService;
    @Resource
    PackResult packResult;
    @Resource
    ContentService contentService;
    @Resource
    ExamService examService;
    @Resource
    AnswerService answerService;

    @Resource
    Upload upload;

    /**
     * 添加试题
     *
     * @param grade
     * @param subjectName
     * @return
     */
    @RequestMapping("/addQuestion")
    public Map<String, Object> addQuestion(@RequestParam(value = "grade", required = false) String grade, @RequestParam(value = "level", required = false) String level, @RequestParam("subjectName") String subjectName, @RequestParam("tagsName")
            String tagsName, @RequestParam("title") String title, @RequestParam("typeName") String typeName, @RequestParam("content") String content, @RequestParam("answer") String answer, @RequestParam(value = "analysis") String analysis) {
        content = content.replace(" ", "");
        try {

            /**
             * 验证是否提交过一样的题目
             */
            if (contentService.notHasContent(content)) {
                Question question = new Question();
                question.setGrade(grade);
                question.setLevel(level);
                question.setTitle(title);

                Subject subject = subjectService.getBySubjectName(subjectName);
                Tags tags = tagsService.getByTagsName(tagsName);

                if (subject == null) {
                    /**
                     * 创建新的科目
                     */
                    subject = getNewSubject(subjectName, 1);
                    subjectService.save(subject);


                } else {
                    subject.setTotalQuestion(subject.getTotalQuestion() + 1);
                    subjectService.change(subject);
                }
                if (tags == null) {
                    /**
                     * 创建新的标签
                     */
                    tags = getNewTags(tagsName, subject.getSubjectId());
                    tagsService.save(tags);
                }
                QuestionType type = questionTypeService.getByTypeAndSubject(typeName, subject.getSubjectId());
                if (type == null) {
                    type = getNewType(typeName, subject.getSubjectId(), 1);
                    questionTypeService.save(type);
                } else {
                    type.setQuestionCount(type.getQuestionCount() + 1);
                    questionTypeService.change(type);
                }

                question.setTagsId(tags.getTagsId());
                question.setSubjectId(subject.getSubjectId());
                question.setTypeId(type.getQuestionTypeId());

                questionService.save(question);

                /**
                 * 创建题目
                 */
                Content c = getNewContent(content, question.getQuestionId());
                contentService.save(c);

                /**
                 * 创建解析
                 */
                Answer a = getNewAnswer(analysis, answer, question.getQuestionId());
                answerService.save(a);
                System.out.println("");
                return packResult.doPackToMap(ResultState.SUCCESS);
            }
            return packResult.doPackToMap(ResultState.EXIST_QUESTION);
        } catch (Exception e) {
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR);
        }
    }

    @RequestMapping("/addSubject")
    public Map<String, Object> addSubject(@RequestParam("subjectName") String subjectName) {
        try {
            Subject subject = subjectService.getBySubjectName(subjectName);
            if (subject == null) {
                subject = getNewSubject(subjectName, 0);
                subjectService.save(subject);
                return packResult.doPackToMap(ResultState.SUCCESS);
            } else {
                return packResult.doPackToMap(ResultState.EXIST_SUBJECT);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR, e);

        }

    }


    @RequestMapping("/addTags")
    public Map<String, Object> addTags(@RequestParam("tagsName") String tagsName, @RequestParam("subjectName") String subjectName) {
        try {
            Subject subject = subjectService.getBySubjectName(subjectName);
            if (subject == null) {
                subject = getNewSubject(subjectName, 0);
                subjectService.save(subject);
            }

            Tags tags = tagsService.getByTagsAndSubject(tagsName, subject.getSubjectId());
            if (tags == null) {
                tags = getNewTags(tagsName, subject.getSubjectId());
                tagsService.save(tags);
                return packResult.doPackToMap(ResultState.SUCCESS);
            } else {
                return packResult.doPackToMap(ResultState.EXIST_TAGS);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR, e);
        }
    }

    @RequestMapping("/addQuestionType")
    public Map<String, Object> addQuestionType(@RequestParam("typeName") String typeName, @RequestParam("subjectName") String subjectName) {

        try {
            Subject subject = subjectService.getBySubjectName(subjectName);
            if (subject == null) {
                subject = getNewSubject(subjectName, 0);
                subjectService.save(subject);
            }
            QuestionType questionType = questionTypeService.getByTypeAndSubject(typeName, subject.getSubjectId());
            if (questionType == null) {
                questionType = getNewType(typeName, subject.getSubjectId(), 0);
                questionTypeService.save(questionType);
                return packResult.doPackToMap(ResultState.SUCCESS);
            } else {
                return packResult.doPackToMap(ResultState.EXIST_TYPE);
            }


        } catch (Exception e) {
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR, e);
        }


    }


    @RequestMapping("/changeSubjectImage")
    private Map<String,Object> changeSubjectImage(@RequestParam("subjectName")String subjectName, @RequestParam("file")MultipartFile file){
        try {
            Subject subject= subjectService.getBySubjectName(subjectName);
            if(subject==null){
                return packResult.doPackToMap(ResultState.NOT_FOUND_SUBJECT);
            }
            subject.setSubjectImage(upload.upload(file,"images","subject",subject.getSubjectId()));

            subjectService.change(subject);
            return packResult.doPackToMap(ResultState.SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR,e);
        }


    }

    private Content getNewContent(String content, Integer questionId) {
        Content c = new Content();
        c.setContent(content);
        c.setQuestionId(questionId);
        return c;
    }

    private Tags getNewTags(String tagsName, Integer subjectId) {
        Tags tags = new Tags();
        tags.setTagsName(tagsName);
        tags.setSubjectId(subjectId);
        return tags;
    }

    private Subject getNewSubject(String subjectName, Integer count) {
        Subject subject = new Subject();
        subject.setSubjectName(subjectName);
        subject.setTotalQuestion(count);
        return subject;
    }

    private QuestionType getNewType(String typeName, Integer subjectId, Integer count) {
        QuestionType questionType = new QuestionType();
        questionType.setQuestionCount(count);
        questionType.setSubjectId(subjectId);
        questionType.setTypeName(typeName);
        return questionType;
    }

    private Answer getNewAnswer(String analysis, String answer, Integer questionId) {
        Answer a = new Answer();
        a.setAnalysis(analysis);
        a.setAnswer(answer);
        a.setQuestionId(questionId);
        return a;
    }


}
